﻿using QD.SQLHelper;
using QD.Tools;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QD.DbManager
{
    public partial class DatabasesMag : DbBasic
    {
        EFDbContext dbContext;
        LogonConfig logonConfig;

        public DatabasesMag(LogonConfig logonConfig)
        {
            InitializeComponent();

            this.logonConfig = logonConfig;

            DataGridViewHelper.InitGrid(this.grid);
            this.grid.Columns["database_id"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            this.grid_files.AutoGenerateColumns = false;
        }

        private void DatabasesMag_Load(object sender, EventArgs e)
        {
            dbContext = new EFDbContext(logonConfig.GetConnectionStrings("master"));

            Grid_DataBind();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Grid_DataBind();
        }

        private void Grid_DataBind()
        {
            string tableName = this.txtDbName.Text.Trim();
            string condition = string.Empty;
            if (tableName.Length > 0)
            {
                if (!QD.Tools.ValueTypeHelper.IsInt(tableName))
                {
                    condition = string.Format("name like '%{0}%'", tableName);
                }
                else
                {
                    condition = string.Format("object_id ={0}", tableName);
                }
            }
            IList<SysDatabases> tables = dbContext.Databases(condition);
            this.grid.DataSource = tables;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            DbLogSizeMag frm = new DbLogSizeMag(logonConfig);
            frm.ShowDialog();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                IList<int> selectRowsIndexs = this.grid.GetSelectRowsIndex();
                if (selectRowsIndexs.Count() == 0)
                {
                    PromptHelper.Prompt("请选择需要查看表空间的数据库！");
                }
                else
                {
                    string dbName = this.grid.Rows[selectRowsIndexs[0]].GetCellValueToString("name");
                    DbTableSizeMag frm = new DbTableSizeMag(logonConfig, dbName);
                    frm.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                PromptHelper.Prompt(ex);
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            try
            {
                IList<int> selectRowsIndexs = this.grid.GetSelectRowsIndex();
                if (selectRowsIndexs.Count() == 0)
                {
                    PromptHelper.Prompt("请选择需要查看表空间的数据库！");
                }
                else
                {
                    string dbName = this.grid.Rows[selectRowsIndexs[0]].GetCellValueToString("name");

                    DialogResult result = PromptHelper.PromptOkCancel("确定收缩数据库[" + dbName + "]");
                    if (result == DialogResult.OK)
                    {
                        dbContext.ShrinkDatabase(logonConfig.GetConnectionStrings("master"), dbName);
                        PromptHelper.Prompt("收缩成功！");
                    }
                }
            }
            catch (Exception ex)
            {
                PromptHelper.Prompt(ex);
            }
        }

        private void txtDbName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.button2_Click(sender, e);
            }
        }

        private void grid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                string database_id = this.grid.Rows[e.RowIndex].Cells["database_id"].Value.ToString();
                string sql = "select * from sys.master_files where database_id=" + database_id;

                DataTable dt = dbContext.Query(sql, false, logonConfig.GetConnectionStrings("master"));
                this.grid_files.DataSource = dt;
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (PromptHelper.PromptOkCancel("确定设置所有数据库为自动收缩！") == DialogResult.OK)
            {
                dbContext.Auto_shrink(logonConfig.GetConnectionStrings("master"));
            }
        }
    }
}
